﻿using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace CMS6Tools.Management
{
    public partial class SelectChangesetForm : Form
    {
        #region Ctor
        public SelectChangesetForm(TfsTeamProjectCollection collection, string itemPath)
        {
            InitializeComponent();

            AuthenticatedTfsTeamProjectCollection = collection;
            ItemPath = itemPath;
            IsCancelled = false;
        }
        #endregion

        #region Porperties
        public string ItemPath { get; private set; }

        public TfsTeamProjectCollection AuthenticatedTfsTeamProjectCollection { get; private set; }

        public bool IsCancelled { get; private set; }

        public Changeset SelectedChangeset { get; private set; }
        
        #endregion

        #region Events

        private void FindButton_Click(object sender, EventArgs e)
        {
            SourceCodeFolderTextBox.Text = ItemPath;


            var versionControlService = AuthenticatedTfsTeamProjectCollection.GetService<VersionControlServer>();

            var queryParameters = new QueryHistoryParameters(SourceCodeFolderTextBox.Text, RecursionType.Full);

            var versionStart = new DateVersionSpec(StartDateTimePicker.Value);
            queryParameters.VersionStart = versionStart;

            var versionEnd = new DateVersionSpec(EndDateTimePicker.Value);
            queryParameters.VersionEnd = versionEnd;

            var changesets = versionControlService.QueryHistory(queryParameters);
            ChangesetListBox.Items.Clear();
            foreach (var changeset in changesets)
            {
                ChangesetListBox.Items.Add(new ChangesetModel(changeset));
            }

            if (changesets.Any())
            {
                HistoryTotalLabel.Text = string.Format("Found {0} records", changesets.Count());
            }
            else
            {
                HistoryTotalLabel.Text = "Not found history.";
            }
        }
         

        private void SelectChangesetForm_Load(object sender, EventArgs e)
        {
            SourceCodeFolderTextBox.Text = ItemPath;
            StartDateTimePicker.CustomFormat = "MM/dd/yyyy HH:mm:ss";
            StartDateTimePicker.Value = DateTime.Now.AddDays(-7);
            EndDateTimePicker.CustomFormat = "MM/dd/yyyy HH:mm:ss";
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            var changesetModel = ChangesetListBox.SelectedItem as ChangesetModel;
            if (changesetModel == null)
            {
                MessageBox.Show("Please select a changeset.");
                return;
            }

            SelectedChangeset = changesetModel == null ? null : changesetModel.Changeset;
            Close();
        }

        private void CancelButton_Click(object sender, EventArgs e)
        {
            IsCancelled = true;
            Close();
        }

        private void ChangesetListBox_DoubleClick(object sender, EventArgs e)
        {
            var changesetModel = ChangesetListBox.SelectedItem as ChangesetModel;
            if (changesetModel == null)
            {
                MessageBox.Show("Please select a changeset.");
                return;
            }

            SelectedChangeset = changesetModel == null ? null : changesetModel.Changeset;
            Close();
        }
        #endregion

        #region ChangesetModel
        internal class ChangesetModel
        {
            public ChangesetModel(Changeset changeset)
            {
                Changeset = changeset;
            }

            public Changeset Changeset { get; private set; }

            public override string ToString()
            {
                return string.Format("{0} - {1} - {2} - {3}", Changeset.ChangesetId, Changeset.CreationDate, Changeset.CommitterDisplayName, Changeset.Comment);
            }
        }
        #endregion

    }
}
